import MMMM.BuildIndex as build
from MMMM.sql import connect_search,get_enfile
from MMMM.TrapDoor import get_token
from MMMM.search_0 import get_column
import ast

prime = 148300296503541537023967543385571281566188269121696986355794642903163978311488366178620508621203382790644644256351530344017086776119734526929467544594283880389370982620148075975876610977802799762624795305966277480303398775064011730140292446255046328316204433780624821532022148316432675132662755400581378870583
key_s = b'\x1a\xde\x9b\xda\xadNq\xf4\xa5\x04g\x02\x99E\x8d\xcf[\x0f\xa6Dx\xe7\t\xbb\x1e\xb2n\xbd|h\xcf\x16'
key_m_1 = b'\xba\x8a\xed\x17y\x8f2[y]{\xd7\x1a\xb8\x05\x08\xf2CL\xe8\xdaY\xa2\xd0o=\x87\xc5De\xb1\x95'
key_m_2= '8bjp6lyrgfn370i5'
key_1 = b'\xaa%\x00"\x08)+\x84\xf1I\x0e\xeb\xb6\xb2\xd4L\x8eBd\xbc{\xc1\xc7\xc9\xd3`\xb1\xfb\xbbZ\xa2\xed'
iv = 'gvmr10z4pc6xqn3l'
iv_bytes = bytes(iv,encoding='utf8')


def dosearch(text):
    unique_words = build.get_unique_word('D:/Dtest/bysms/MMMM/test.txt')
    sentenses = build.get_sentense('D:/Dtest/bysms/MMMM/test.txt')
    Ir = build.get_Ir(key_m_1, unique_words)
    Ic = build.get_Ic(key_m_2.encode('utf-8'), iv_bytes, sentenses)
    I = build.get_I(prime, key_s, key_1, sentenses, unique_words, Ic, Ir)
    s1 = build.get_f(sentenses)
    I_2 = build.expand(s1, I, sentenses, unique_words)

    print(I)
    s1 = build.get_f(sentenses)

    text = text.lower()
    words = text.split()
    words = [word.strip('.,!;()[]') for word in words]

    T = get_token(words)
    column, column_msk = get_column(prime, T,Ir)

    connect_search(column, column_msk, T, Ic, Ir, I, s1)

    encrypted_file = get_enfile()
    encrypted_file = encrypted_file.strip('*')
    encrypted_file = ast.literal_eval(encrypted_file)
    print(encrypted_file)
    value = []
    for i in range(0, len(encrypted_file)):
        plaintext = build.sm4_cbc_dec(encrypted_file[i][0], iv_bytes, key_m_2.encode('utf-8'))
        value.append(plaintext)
    '''print("------------所在内容------------")
    for i in range(0, len(value)):
        print(value[i].decode('utf-8'))'''

    return value



